#include "stdafx.h"
#include "GameGUI.h"

GameGUI::GameGUI(GuiManager* gui_manager) : m_gui_manager(gui_manager){}
GameGUI::~GameGUI(void)
{
	for (auto it = m_buttons.begin(); it != m_buttons.end(); it++)
	{
		delete it->second;
		it->second = nullptr;
	}
	for (auto it = m_textfields.begin(); it != m_textfields.end(); it++)
	{
		delete it->second;
		it->second = nullptr;
	}
}

Gui::Button* GameGUI::CreateButton(Gui::EButtonType type, int x, int y, int w, int h, const String& text, unsigned int color, int font_size)
{
	Gui::Button* button = new Gui::Button;
	button->SetWidth(w);
	button->SetHeight(h);
	button->SetX(x);
	button->SetY(y);
	button->SetText(text);
	button->SetColor(color);
	button->SetFontSize(font_size);
	button->SetFontId(1);
	button->SetButtonType(type);

	m_gui_manager->AttachWidget(button);
	m_buttons.insert(std::make_pair(type, button));

	return button;
}

Gui::Textfield* GameGUI::CreateTextfield(ETextfield_Type type, int x, int y, int w, int h, const String& text, unsigned int color, int font_size, bool visible)
{
	Gui::Textfield* textfield = new Gui::Textfield;
	textfield->SetWidth(w);
	textfield->SetHeight(h);
	textfield->SetX(x);
	textfield->SetY(y);
	textfield->SetText(text);
	textfield->SetColor(color);
	textfield->SetFontSize(font_size);
	textfield->SetFontId(1);
	textfield->SetVisible(visible);

	m_gui_manager->AttachWidget(textfield);
	m_textfields.insert(std::make_pair(type, textfield));


	return textfield;
}

Gui::Button* GameGUI::GetButton(Gui::EButtonType type)
{
	auto it = m_buttons.find(type);

	if (it != m_buttons.end())
		return (*it).second;

	return nullptr;
}

Gui::Button* GameGUI::GetButton(float x, float y)
{
	Gui::Button* btn = nullptr;

	for (auto it = m_buttons.begin(); it != m_buttons.end(); it++)
	{
		if (it->second->IsInside(x, y))
		{
			btn = it->second;
			// Green highlight
			it->second->SetColor(0x00aa00aa);
		}
		else
		{
			it->second->SetColor(0x00000000);
		}
	}

	if (btn)
		return btn;

	return nullptr;
}

Gui::Textfield* GameGUI::GetTextfield(ETextfield_Type type)
{
	auto it = m_textfields.find(type);

	if (it != m_textfields.end())
		return (*it).second;

	return nullptr;
}